<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\admin\Controller;
use think\facade\Session;
use think\facade\Request;
use think\facade\Filesystem;


use think\admin\Storage;
use think\admin\storage\AliossStorage;
use think\admin\storage\LocalStorage;
use think\admin\storage\QiniuStorage;
use think\admin\storage\TxcosStorage;
use think\file\UploadedFile;
use think\Response;
use think\response\Json;

use app\index\service\ExcelService;
use app\index\service\ConfigService;

/**
 * Class Index
 * @package app\index\controller
 */
class Index extends Controller
{
    
    //初始化配置
    protected $table = 'Excel';//表名
    // 初始化

    protected function initialize()
    {
        $this->config = config('excel');//调用配置文件示例
    }
    
    public function index()
    {
        //halt("xuanranccc");
        //halt($this->config['fileSize']);
        // halt($this->config['fileext']);
        $this->fetch();
        //$this->redirect(sysuri('admin/login/index'));
    }

    public function t()
    {
        //halt("xuanranccc");
        //halt($this->config['fileSize']);
        // halt($this->config['fileext']);1
        $this->fetch('test');
        //$this->redirect(sysuri('admin/login/index'));
    }


    /**
    * upload 上传接口
    * @author    saruri <saruri@163.com>
    * @date      2021/02/13 22:58:58
    * @auth false
    * @menu false
    * @login false
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function upload()
    {
        //do somehing
        $file = Request::file('file');
        //halt($file);
        //验证文件规则
        $result=validate(['file' => ['fileSize:'.$this->config['fileSize'].',fileExt:'.$this->config['fileExt']]])->check(['file' => $file]);
        if ($result) {
            //上传到服务器,
            $path = Filesystem::disk('public')->putFile('upload', $file);
            //结果是 $path = upload/20200825\***.jpg
            
            //图片路径，Filesystem::getDiskConfig('public','url')功能是获取public目录下的storage，
            $picCover = Filesystem::getDiskConfig('public', 'url').'/'.str_replace('\\', '/', $path);
            //结果是 $picCover = storage/upload/20200825/***.jpg
            
            //获取图片名称
            $fileName = $file->getOriginalName();
            $arr['filename']= $fileName;
            $filepath=$this->config['basePath'];
            $arr['md5'] = md5_file($filepath.'../public/'.$picCover);
        }
        
        $arr['result']=$result;
        $arr['pic']= $picCover;
        $arr['headLine']=input('headLine');
        //halt($arr);
       
        //halt($arr);
        $data=ExcelService::instance()->excelCtrl($arr)->excelHead()->getHeadList()->run();
        $this->result['data']=$data;
        unset($this->result['data']['param']['pic']);
        // $this->excelCtrl($arr)->excelHead();
        //判断redis
        //excel操作
        //返回json
        exit(json_encode($this->result));


        //文件放在了excelReader\public\static\目录下，文件名shuju.xlsx
        //$objPHPExcel = PHPExcel_IOFactory::load(public_path().$picCover);
       
       
        //halt($spreadsheet);
        //获取表名，一维数组，值是表名。如：array('sheet1', 'sheet2', 'sheet3')
        //$nameArr = $objPHPExcel->getsheetnames();
        //$arr['sheepname']=$nameArr;

        exit(json_encode($arr));
        exit("这里是:");
    }


    /**
    * 保存配置
    * @author    saruri <saruri@163.com>
    * @date      2021/02/18 23:52:19
    * @auth false
    * @menu false
    * @login false
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function saveConfig()
    {
        //do somehing
        $arr['config']=input('config');
        $arr['tab']=input('tab');
        $arr['md5']= input('md5');
        $arr['content']=input('fields2');
        $arr['head']=input('head');
        $arr['create_time']=time();
       
        foreach ($arr as $key => $value) {
            if ($value=='') {
                $data['status']=0;
                $data['msg']=$key .'参数为必填 请检查';
                exit(json_encode($data));
            }
        }
        $re=ConfigService::instance()->save($arr);
        if ($re===false) {
            $data['status']=0;
            $data['msg']='已经有相同配置了';
            exit(json_encode($data));
        }

        $data['status']=1;
        $data['msg']='添加成功';
        exit(json_encode($data));
    }
}
